<template>
  <!-- // 分配角色 -->
  <div>
    <template>
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
    </template>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    // 请求角色列表
    this.loadRoles()
  },
  methods: {
    //
    // 请求角色列表
    async loadRoles() {
      // 发送ajax请求
      const res = await getRoles({ page: 1, pagesize: 10000 })
      console.log('请求角色列表', res)
      // 渲染数据
      const info = await getUserDetailById(this.employeeId)
      console.log('获取员工基本信息', info)
      this.roleIds = info.data.roleIds
      this.roleList = res.data.rows
    },
    closeDialog() {
      this.$emit('close')
    },
    async hSubmit() {
      const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      console.log('分配角色接口', res)
      this.$emit('success')
    }
  }
}
</script>
